<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>清晰账本 - 交易记录</title>
    <link rel="stylesheet" href="glassmorphism.css">
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Arial', sans-serif;
        }
        
        body {
            background: linear-gradient(45deg, #6a11cb, #2575fc);
            min-height: 100vh;
            color: #fff;
            padding-bottom: 80px;
        }
        
        .app-container {
            max-width: 480px;
            margin: 0 auto;
            padding: 20px;
        }
        
        .header {
            display: flex;
            align-items: center;
            margin-bottom: 20px;
            position: relative;
        }
        
        .back-button {
            position: absolute;
            left: 0;
            width: 40px;
            height: 40px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            background: rgba(255, 255, 255, 0.1);
            backdrop-filter: blur(5px);
            border: 1px solid rgba(255, 255, 255, 0.2);
            text-decoration: none;
            color: white;
        }
        
        .page-title {
            flex: 1;
            text-align: center;
            font-size: 20px;
        }
        
        .filter-button {
            position: absolute;
            right: 0;
            width: 40px;
            height: 40px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            background: rgba(255, 255, 255, 0.1);
            backdrop-filter: blur(5px);
            border: 1px solid rgba(255, 255, 255, 0.2);
            cursor: pointer;
        }
        
        .month-selector {
            display: flex;
            align-items: center;
            justify-content: space-between;
            background: rgba(255, 255, 255, 0.1);
            backdrop-filter: blur(5px);
            border: 1px solid rgba(255, 255, 255, 0.2);
            border-radius: 20px;
            padding: 10px 15px;
            margin-bottom: 20px;
        }
        
        .current-month {
            font-size: 16px;
            font-weight: bold;
        }
        
        .arrow-button {
            width: 30px;
            height: 30px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            background: rgba(255, 255, 255, 0.1);
            border: 1px solid rgba(255, 255, 255, 0.2);
            cursor: pointer;
        }
        
        .transaction-group {
            margin-bottom: 20px;
        }
        
        .group-date {
            font-size: 14px;
            margin-bottom: 10px;
            opacity: 0.8;
        }
        
        .transaction-item {
            display: flex;
            align-items: center;
            background: rgba(255, 255, 255, 0.1);
            backdrop-filter: blur(5px);
            border: 1px solid rgba(255, 255, 255, 0.2);
            border-radius: 15px;
            padding: 15px;
            margin-bottom: 10px;
            transition: transform 0.3s;
            cursor: pointer;
        }
        
        .transaction-item:hover {
            transform: translateY(-2px);
        }
        
        .transaction-icon {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            background: rgba(255, 255, 255, 0.15);
            display: flex;
            align-items: center;
            justify-content: center;
            margin-right: 15px;
        }
        
        .transaction-details {
            flex: 1;
        }
        
        .transaction-title {
            font-size: 16px;
            margin-bottom: 5px;
        }
        
        .transaction-category {
            font-size: 12px;
            opacity: 0.7;
        }
        
        .transaction-amount {
            font-weight: bold;
        }
        
        .expense-amount {
            color: #ff5e62;
        }
        
        .income-amount {
            color: #26de81;
        }

        .empty-state {
            text-align: center;
            padding: 40px 0;
            display: none;
        }

        .empty-icon {
            font-size: 64px;
            margin-bottom: 20px;
            opacity: 0.5;
        }

        .empty-text {
            font-size: 16px;
            opacity: 0.7;
        }

        .add-button-container {
            position: fixed;
            bottom: 80px;
            right: 20px;
            z-index: 100;
        }
        
        .float-add-button {
            width: 60px;
            height: 60px;
            border-radius: 50%;
            background: rgba(255, 255, 255, 0.2);
            backdrop-filter: blur(5px);
            display: flex;
            align-items: center;
            justify-content: center;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
            border: 1px solid rgba(255, 255, 255, 0.3);
            color: white;
            text-decoration: none;
            font-size: 24px;
        }
        
        .filter-menu {
            position: fixed;
            top: 0;
            right: 0;
            bottom: 0;
            background: rgba(35, 35, 35, 0.95);
            backdrop-filter: blur(10px);
            width: 80%;
            max-width: 320px;
            z-index: 1000;
            padding: 20px;
            transform: translateX(100%);
            transition: transform 0.3s;
            color: white;
        }
        
        .filter-menu.active {
            transform: translateX(0);
        }
        
        .filter-header {
            display: flex;
            align-items: center;
            justify-content: space-between;
            margin-bottom: 30px;
        }
        
        .filter-title {
            font-size: 20px;
            font-weight: bold;
        }
        
        .close-filter {
            width: 30px;
            height: 30px;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
        }
        
        .filter-section {
            margin-bottom: 25px;
        }
        
        .filter-label {
            font-size: 16px;
            margin-bottom: 15px;
        }
        
        .filter-options {
            display: flex;
            gap: 10px;
        }
        
        .filter-option {
            flex: 1;
            text-align: center;
            padding: 10px;
            border-radius: 10px;
            background: rgba(255, 255, 255, 0.1);
            cursor: pointer;
        }
        
        .filter-option.active {
            background: rgba(255, 255, 255, 0.25);
        }
        
        .category-filters {
            display: grid;
            grid-template-columns: repeat(3, 1fr);
            gap: 10px;
            margin-bottom: 15px;
        }
        
        .filter-category {
            border-radius: 10px;
            padding: 10px 5px;
            text-align: center;
            background: rgba(255, 255, 255, 0.1);
            cursor: pointer;
            font-size: 12px;
        }
        
        .filter-category.active {
            background: rgba(255, 255, 255, 0.25);
        }
        
        .amount-range {
            padding: 0 10px;
        }
        
        .amount-inputs {
            display: flex;
            align-items: center;
            gap: 10px;
            margin-top: 15px;
        }
        
        .amount-input {
            flex: 1;
            padding: 10px;
            border-radius: 5px;
            background: rgba(255, 255, 255, 0.1);
            border: 1px solid rgba(255, 255, 255, 0.2);
            color: white;
        }
        
        .filter-actions {
            display: flex;
            gap: 10px;
        }
        
        .filter-action {
            flex: 1;
            padding: 15px;
            text-align: center;
            border-radius: 10px;
            cursor: pointer;
        }
        
        .reset-filter {
            background: rgba(255, 94, 98, 0.2);
        }
        
        .apply-filter {
            background: rgba(38, 222, 129, 0.2);
        }
        
        .navigation {
            position: fixed;
            bottom: 0;
            left: 0;
            right: 0;
            display: flex;
            justify-content: space-around;
            padding: 15px 0;
            background: rgba(255, 255, 255, 0.1);
            backdrop-filter: blur(10px);
            border-top: 1px solid rgba(255, 255, 255, 0.2);
            z-index: 100;
        }
        
        .nav-item {
            display: flex;
            flex-direction: column;
            align-items: center;
            color: rgba(255, 255, 255, 0.7);
            text-decoration: none;
            transition: color 0.3s;
        }
        
        .nav-item.active {
            color: #fff;
        }
        
        .add-button {
            width: 60px;
            height: 60px;
            border-radius: 50%;
            background: rgba(255, 255, 255, 0.2);
            backdrop-filter: blur(5px);
            display: flex;
            align-items: center;
            justify-content: center;
            margin-top: -40px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
            border: 1px solid rgba(255, 255, 255, 0.3);
        }

        .delete-modal {
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: rgba(0, 0, 0, 0.5);
            display: none;
            align-items: center;
            justify-content: center;
            z-index: 1000;
        }
        
        .delete-modal-content {
            width: 80%;
            max-width: 300px;
            background: rgba(35, 35, 35, 0.95);
            backdrop-filter: blur(15px);
            border-radius: 15px;
            padding: 25px;
            text-align: center;
        }
        
        .delete-title {
            font-size: 18px;
            margin-bottom: 15px;
        }
        
        .delete-message {
            font-size: 14px;
            opacity: 0.8;
            margin-bottom: 25px;
        }
        
        .delete-actions {
            display: flex;
            justify-content: space-between;
        }
        
        .delete-btn {
            flex: 1;
            padding: 12px;
            border-radius: 10px;
            cursor: pointer;
            text-align: center;
            font-size: 14px;
            margin: 0 5px;
        }
        
        .delete-cancel {
            background: rgba(255, 255, 255, 0.1);
        }
        
        .delete-confirm {
            background: rgba(255, 94, 98, 0.3);
        }

        .overlay {
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: rgba(0, 0, 0, 0.5);
            backdrop-filter: blur(5px);
            z-index: 900;
            display: none;
        }
        
        .toast {
            position: fixed;
            top: 20px;
            left: 50%;
            transform: translateX(-50%);
            background: rgba(38, 222, 129, 0.2);
            backdrop-filter: blur(10px);
            border: 1px solid rgba(255, 255, 255, 0.2);
            border-radius: 10px;
            padding: 15px 20px;
            color: white;
            font-size: 14px;
            opacity: 0;
            transition: opacity 0.3s;
            z-index: 1000;
        }
        
        .toast.show {
            opacity: 1;
        }
        
        .toast.error {
            background: rgba(255, 94, 98, 0.2);
        }
    </style>
</head>
<body>
    <div class="app-container">
        <div class="header">
            <a href="index.html" class="back-button">
                <i class="material-icons">arrow_back</i>
            </a>
            <div class="page-title">交易记录</div>
            <div class="filter-button" onclick="toggleFilterMenu()">
                <i class="material-icons">filter_list</i>
            </div>
        </div>
        
        <div class="month-selector">
            <div class="arrow-button" id="prevMonthBtn">
                <i class="material-icons">chevron_left</i>
            </div>
            <div class="current-month" id="currentMonth">2023年5月</div>
            <div class="arrow-button" id="nextMonthBtn">
                <i class="material-icons">chevron_right</i>
            </div>
        </div>
        
        <div id="transactionList">
            <!-- 交易列表将通过JavaScript动态填充 -->
        </div>
        
        <div class="empty-state" id="emptyState">
            <div class="empty-icon">
                <i class="material-icons">receipt_long</i>
            </div>
            <div class="empty-text">暂无交易记录</div>
        </div>
        
        <div class="add-button-container">
            <a href="add_transaction.html" class="float-add-button">
                <i class="material-icons">add</i>
            </a>
        </div>
    </div>
    
    <div class="filter-menu" id="filterMenu">
        <div class="filter-header">
            <div class="filter-title">筛选交易</div>
            <div class="close-filter" onclick="toggleFilterMenu()">
                <i class="material-icons">close</i>
            </div>
        </div>
        
        <div class="filter-section">
            <div class="filter-label">交易类型</div>
            <div class="filter-options" id="typeFilter">
                <div class="filter-option active" data-type="all">全部</div>
                <div class="filter-option" data-type="expense">支出</div>
                <div class="filter-option" data-type="income">收入</div>
            </div>
        </div>
        
        <div class="filter-section">
            <div class="filter-label">分类</div>
            <div class="category-filters" id="categoryFilter">
                <!-- 分类选项将通过JavaScript动态填充 -->
            </div>
        </div>
        
        <div class="filter-section">
            <div class="filter-label">金额范围</div>
            <div class="amount-range">
                <div class="amount-inputs">
                    <input type="number" class="amount-input" id="minAmount" placeholder="最小金额">
                    <span>-</span>
                    <input type="number" class="amount-input" id="maxAmount" placeholder="最大金额">
                </div>
            </div>
        </div>
        
        <div class="filter-actions">
            <div class="filter-action reset-filter" id="resetFilterBtn">重置</div>
            <div class="filter-action apply-filter" id="applyFilterBtn">应用</div>
        </div>
    </div>
    
    <div class="delete-modal" id="deleteModal">
        <div class="delete-modal-content">
            <div class="delete-title">删除交易</div>
            <div class="delete-message">确定要删除这笔交易记录吗？此操作无法撤销。</div>
            <div class="delete-actions">
                <div class="delete-btn delete-cancel" onclick="closeDeleteModal()">取消</div>
                <div class="delete-btn delete-confirm" id="confirmDeleteBtn">删除</div>
            </div>
        </div>
    </div>
    
    <div class="overlay" id="overlay"></div>
    
    <div class="toast" id="toast">操作成功</div>
    
    <div class="navigation">
        <a href="index.html" class="nav-item">
            <i class="material-icons">home</i>
            <span>首页</span>
        </a>
        <a href="transactions.html" class="nav-item active">
            <i class="material-icons">receipt_long</i>
            <span>交易</span>
        </a>
        <a href="statistics.html" class="nav-item">
            <i class="material-icons">pie_chart</i>
            <span>统计</span>
        </a>
        <a href="my_profile.html" class="nav-item">
            <i class="material-icons">person</i>
            <span>我的</span>
        </a>
    </div>
    
    <script src="app_data.js"></script>
    <script>
        // 全局变量
        let currentDate = new Date();
        let filteredTransactions = [];
        let currentTransactionId = null;
        
        // 过滤条件
        let filters = {
            type: 'all',
            categories: [],
            minAmount: null,
            maxAmount: null
        };
        
        // DOM元素
        const transactionList = document.getElementById('transactionList');
        const emptyState = document.getElementById('emptyState');
        const currentMonthElement = document.getElementById('currentMonth');
        const prevMonthBtn = document.getElementById('prevMonthBtn');
        const nextMonthBtn = document.getElementById('nextMonthBtn');
        const filterMenu = document.getElementById('filterMenu');
        const overlay = document.getElementById('overlay');
        const typeFilterOptions = document.querySelectorAll('#typeFilter .filter-option');
        const categoryFilterContainer = document.getElementById('categoryFilter');
        const minAmountInput = document.getElementById('minAmount');
        const maxAmountInput = document.getElementById('maxAmount');
        const resetFilterBtn = document.getElementById('resetFilterBtn');
        const applyFilterBtn = document.getElementById('applyFilterBtn');
        const deleteModal = document.getElementById('deleteModal');
        const confirmDeleteBtn = document.getElementById('confirmDeleteBtn');
        const toast = document.getElementById('toast');
        
        // 初始化应用
        function initializeApp() {
            // 设置当前月份
            updateMonthDisplay();
            
            // 载入交易数据
            loadTransactions();
            
            // 渲染分类过滤器
            renderCategoryFilters();
            
            // 添加事件监听
            addEventListeners();
        }
        
        // 添加事件监听
        function addEventListeners() {
            // 月份切换事件
            prevMonthBtn.addEventListener('click', navigateToPreviousMonth);
            nextMonthBtn.addEventListener('click', navigateToNextMonth);
            
            // 类型过滤事件
            typeFilterOptions.forEach(option => {
                option.addEventListener('click', function() {
                    typeFilterOptions.forEach(opt => opt.classList.remove('active'));
                    this.classList.add('active');
                    filters.type = this.dataset.type;
                });
            });
            
            // 过滤按钮事件
            resetFilterBtn.addEventListener('click', resetFilters);
            applyFilterBtn.addEventListener('click', applyFilters);
            
            // 确认删除按钮事件
            confirmDeleteBtn.addEventListener('click', confirmDelete);
        }
        
        // 加载交易数据
        function loadTransactions() {
            const transactionManager = new window.AppData.TransactionManager();
            const transactions = transactionManager.getTransactions();
            
            // 按月过滤交易
            const filteredByMonth = filterTransactionsByMonth(transactions, currentDate);
            
            // 应用其他过滤条件
            filteredTransactions = applyFilters(filteredByMonth);
            
            // 渲染交易列表
            renderTransactions(filteredTransactions);
        }
        
        // 按月过滤交易
        function filterTransactionsByMonth(transactions, date) {
            const year = date.getFullYear();
            const month = date.getMonth();
            
            return transactions.filter(transaction => {
                const transactionDate = new Date(transaction.date);
                return transactionDate.getFullYear() === year && transactionDate.getMonth() === month;
            });
        }
        
        // 应用过滤器
        function applyFilters(transactions) {
            return transactions.filter(transaction => {
                // 类型过滤
                if (filters.type !== 'all' && transaction.type !== filters.type) {
                    return false;
                }
                
                // 分类过滤
                if (filters.categories.length > 0 && !filters.categories.includes(transaction.categoryId)) {
                    return false;
                }
                
                // 金额过滤
                if (filters.minAmount !== null && transaction.amount < filters.minAmount) {
                    return false;
                }
                
                if (filters.maxAmount !== null && transaction.amount > filters.maxAmount) {
                    return false;
                }
                
                return true;
            });
        }
        
        // 渲染交易列表
        function renderTransactions(transactions) {
            // 如果没有交易，显示空状态
            if (transactions.length === 0) {
                transactionList.innerHTML = '';
                emptyState.style.display = 'block';
                return;
            }
            
            // 隐藏空状态
            emptyState.style.display = 'none';
            
            // 按日期分组交易
            const groupedTransactions = groupTransactionsByDate(transactions);
            
            // 构建HTML
            let html = '';
            
            Object.keys(groupedTransactions).forEach(date => {
                const transactions = groupedTransactions[date];
                
                html += `
                    <div class="transaction-group">
                        <div class="group-date">${formatDate(date)}</div>
                `;
                
                transactions.forEach(transaction => {
                    // 获取分类信息
                    const category = getCategoryById(transaction.categoryId, transaction.type);
                    
                    // 金额样式
                    const amountClass = transaction.type === 'expense' ? 'expense-amount' : 'income-amount';
                    const amountPrefix = transaction.type === 'expense' ? '-' : '+';
                    
                    html += `
                        <div class="transaction-item" data-id="${transaction.id}" onclick="viewTransactionDetail('${transaction.id}')">
                            <div class="transaction-icon">
                                <i class="material-icons">${category ? category.icon : 'help'}</i>
                            </div>
                            <div class="transaction-details">
                                <div class="transaction-title">${transaction.note || (category ? category.name : '未知分类')}</div>
                                <div class="transaction-category">${category ? category.name : '未知分类'}</div>
                            </div>
                            <div class="transaction-amount ${amountClass}">${amountPrefix}${transaction.amount.toFixed(2)}</div>
                        </div>
                    `;
                });
                
                html += `</div>`;
            });
            
            transactionList.innerHTML = html;
        }
        
        // 查看交易详情
        function viewTransactionDetail(transactionId) {
            window.location.href = `transaction_detail.html?id=${transactionId}`;
        }
        
        // 按日期分组交易
        function groupTransactionsByDate(transactions) {
            const grouped = {};
            
            transactions.forEach(transaction => {
                const date = transaction.date.split('T')[0]; // 提取日期部分
                
                if (!grouped[date]) {
                    grouped[date] = [];
                }
                
                grouped[date].push(transaction);
            });
            
            // 按日期倒序排序
            const sortedDates = Object.keys(grouped).sort().reverse();
            const sortedGrouped = {};
            
            sortedDates.forEach(date => {
                sortedGrouped[date] = grouped[date];
            });
            
            return sortedGrouped;
        }
        
        // 格式化日期显示
        function formatDate(dateString) {
            const date = new Date(dateString);
            const today = new Date();
            const yesterday = new Date(today);
            yesterday.setDate(yesterday.getDate() - 1);
            
            // 格式化为yyyy-MM-dd格式
            today.setHours(0, 0, 0, 0);
            yesterday.setHours(0, 0, 0, 0);
            date.setHours(0, 0, 0, 0);
            
            if (date.getTime() === today.getTime()) {
                return '今天';
            } else if (date.getTime() === yesterday.getTime()) {
                return '昨天';
            } else {
                const options = { month: 'long', day: 'numeric', weekday: 'long' };
                return date.toLocaleDateString('zh-CN', options);
            }
        }
        
        // 获取分类信息
        function getCategoryById(categoryId, type) {
            const categories = type === 'expense' ? window.AppData.expenseCategories : window.AppData.incomeCategories;
            return categories.find(category => category.id === categoryId);
        }
        
        // 渲染分类过滤器
        function renderCategoryFilters() {
            // 合并支出和收入分类
            const allCategories = [...window.AppData.expenseCategories, ...window.AppData.incomeCategories];
            
            // 构建HTML
            const html = allCategories.map(category => `
                <div class="filter-category" data-id="${category.id}" onclick="toggleCategoryFilter(this)">
                    ${category.name}
                </div>
            `).join('');
            
            categoryFilterContainer.innerHTML = html;
        }
        
        // 切换分类过滤器
        function toggleCategoryFilter(element) {
            const categoryId = element.dataset.id;
            
            // 更新UI
            element.classList.toggle('active');
            
            // 更新过滤条件
            if (element.classList.contains('active')) {
                filters.categories.push(categoryId);
            } else {
                filters.categories = filters.categories.filter(id => id !== categoryId);
            }
        }
        
        // 重置过滤器
        function resetFilters() {
            // 重置过滤条件
            filters = {
                type: 'all',
                categories: [],
                minAmount: null,
                maxAmount: null
            };
            
            // 重置UI
            typeFilterOptions.forEach(option => {
                option.classList.remove('active');
                if (option.dataset.type === 'all') {
                    option.classList.add('active');
                }
            });
            
            const categoryOptions = document.querySelectorAll('#categoryFilter .filter-category');
            categoryOptions.forEach(option => option.classList.remove('active'));
            
            minAmountInput.value = '';
            maxAmountInput.value = '';
        }
        
        // 应用过滤器
        function applyFilters() {
            // 更新金额过滤条件
            filters.minAmount = minAmountInput.value ? parseFloat(minAmountInput.value) : null;
            filters.maxAmount = maxAmountInput.value ? parseFloat(maxAmountInput.value) : null;
            
            // 重新加载交易
            loadTransactions();
            
            // 关闭过滤菜单
            toggleFilterMenu();
        }
        
        // 切换上一个月
        function navigateToPreviousMonth() {
            currentDate.setMonth(currentDate.getMonth() - 1);
            updateMonthDisplay();
            loadTransactions();
        }
        
        // 切换下一个月
        function navigateToNextMonth() {
            currentDate.setMonth(currentDate.getMonth() + 1);
            updateMonthDisplay();
            loadTransactions();
        }
        
        // 更新月份显示
        function updateMonthDisplay() {
            const year = currentDate.getFullYear();
            const month = currentDate.getMonth() + 1;
            currentMonthElement.textContent = `${year}年${month}月`;
        }
        
        // 切换过滤菜单
        function toggleFilterMenu() {
            filterMenu.classList.toggle('active');
            overlay.style.display = filterMenu.classList.contains('active') ? 'block' : 'none';
        }
        
        // 显示交易操作
        function showTransactionActions(element) {
            const transactionId = element.dataset.id;
            currentTransactionId = transactionId;
            
            // 显示删除确认模态框
            deleteModal.style.display = 'flex';
            overlay.style.display = 'block';
        }
        
        // 关闭删除模态框
        function closeDeleteModal() {
            deleteModal.style.display = 'none';
            overlay.style.display = 'none';
            currentTransactionId = null;
        }
        
        // 确认删除交易
        function confirmDelete() {
            if (!currentTransactionId) return;
            
            const transactionManager = new window.AppData.TransactionManager();
            transactionManager.deleteTransaction(currentTransactionId);
            
            // 重新加载交易
            loadTransactions();
            
            // 关闭模态框
            closeDeleteModal();
            
            // 显示提示
            showToast('交易已删除');
        }
        
        // 显示提示信息
        function showToast(message, isError = false) {
            toast.textContent = message;
            toast.classList.add('show');
            
            if (isError) {
                toast.classList.add('error');
            } else {
                toast.classList.remove('error');
            }
            
            setTimeout(() => {
                toast.classList.remove('show');
            }, 3000);
        }
        
        // 点击空白区域关闭菜单和模态框
        overlay.addEventListener('click', function() {
            if (filterMenu.classList.contains('active')) {
                toggleFilterMenu();
            }
            
            if (deleteModal.style.display === 'flex') {
                closeDeleteModal();
            }
        });
        
        // 初始化应用
        document.addEventListener('DOMContentLoaded', initializeApp);
    </script>
</body>
</html> 